package niumao.minecraft.uhc.indicator;

import niumao.minecraft.uhc.UHCGame;
import niumao.minecraft.uhc.Main;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_14_R1.CraftServer;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ScoreBoardInfo {
    private static final Main plugin = Main.getPlugin(Main.class);
    public static BukkitTask baseTask;
    public static BukkitTask uhcTask;
    public static boolean Flag = true;  //true表示baseTask, false表示uhcTask

    public static void initBase(Player player) {
        baseTask = new BukkitRunnable() {
            @Override
            public void run() {
                if (!Flag) return;
                Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
                Objective objective = scoreboard.registerNewObjective("lobby", "dummy", getStr("baseName"));
                objective.setDisplaySlot(DisplaySlot.SIDEBAR);

                SimpleDateFormat formatter;
                if (Main.getLang().equalsIgnoreCase("en_us"))
                    formatter = new SimpleDateFormat("dd/MM/yy ");
                else formatter = new SimpleDateFormat("yy/MM/dd ");
                Score info = objective.getScore(ChatColor.GRAY + formatter.format(new Date()) + ChatColor.DARK_GRAY + player.getWorld().getName());

                Score a = objective.getScore(ChatColor.GREEN + "TPS: " + (int) ((CraftServer) Bukkit.getServer()).getServer().recentTps[0]);

                Score language;
                if (Main.getLang().equalsIgnoreCase("en_us"))
                    language = objective.getScore("Language: " + "English");
                else language = objective.getScore("当前语言: " + "中文");

                Score version = objective.getScore(ChatColor.YELLOW + Bukkit.getBukkitVersion());

                info.setScore(6);
                objective.getScore(" ").setScore(5);
                a.setScore(4);
                objective.getScore("").setScore(3);
                language.setScore(2);
                version.setScore(1);
                player.setScoreboard(scoreboard);
            }

            @Override
            public synchronized void cancel() throws IllegalStateException {
                super.cancel();
            }
        }.runTaskTimer(Main.getInstance(), 0L, 20L);
    }

    public static void initUHC(Player player) {
        uhcTask = new BukkitRunnable() {
            @Override
            public void run() {
                if (Flag) return;
                World world = player.getWorld();
                Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
                Objective objective = scoreboard.registerNewObjective("uhc", "dummy", getStr("baseName"));
                objective.setDisplaySlot(DisplaySlot.SIDEBAR);

                SimpleDateFormat formatter;
                if (Main.getLang().equalsIgnoreCase("en_us"))
                    formatter = new SimpleDateFormat("dd/MM/yy ");
                else formatter = new SimpleDateFormat("yy/MM/dd ");
                Date date = new Date();
                Score info = objective.getScore(ChatColor.GRAY + formatter.format(date) + ChatColor.DARK_GRAY + player.getWorld().getName());

                Score a;
                Score b;
                a = objective.getScore(getStr("Death"));
                b = objective.getScore(ChatColor.GREEN + String.valueOf(UHCGame.timer) + getStr("Minutes"));

                Score c = objective.getScore(getStr("Players"));
                Score d = objective.getScore(ChatColor.GREEN + String.valueOf(UHCGame.getSurvivals()) + ChatColor.GRAY + "/" + UHCGame.playerList.size());

                Score e = objective.getScore(getStr("Kills") + ChatColor.GREEN + UHCGame.PlayerKills.get(player.getUniqueId()));

                Score f = objective.getScore(getStr("WorldBorder"));
                int worldBorderSize = (int) world.getWorldBorder().getSize() / 2;
                Score g = objective.getScore(ChatColor.GREEN + "-" + worldBorderSize + ", +" + worldBorderSize);

                Score version = objective.getScore(ChatColor.YELLOW + Bukkit.getBukkitVersion());

                info.setScore(14);
                objective.getScore("    ").setScore(13);
                a.setScore(12);
                b.setScore(11);
                objective.getScore("   ").setScore(10);
                c.setScore(9);
                d.setScore(8);
                objective.getScore("  ").setScore(7);
                e.setScore(6);
                objective.getScore(" ").setScore(5);
                f.setScore(4);
                g.setScore(3);
                objective.getScore("").setScore(2);
                version.setScore(1);
                player.setScoreboard(scoreboard);
            }

            @Override
            public synchronized void cancel() throws IllegalStateException {
                super.cancel();
            }
        }.runTaskTimer(Main.getInstance(), 0L, 20L);
    }

    private static String getStr(String name) {
        return plugin.getConfig().getString("ServerInfo." + Main.getLang() + "." + name);
    }
}
